package cloudInformation.controller;

import cloudEntity.utils.ResultUtils;
import cloudEntity.vo.PageBeanVO;
import cloudEntity.vo.news.SmartNewsVO;
import cloudInformation.customer.MinioService;
import cloudInformation.service.SmartNewsService;
import cn.idev.excel.FastExcel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import cloudEntity.dto.news.SmartNewsUpdateDTO;
import cloudEntity.dto.news.SmartNewsInsertDTO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import cloudEntity.vo.news.SmartNewsExcelExportVO;


import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * @program: Smart School
 * @ClassName NewsController
 * @description: 新闻控制器
 * @author: 陈智博
 * @create: 2025-04-14 08:44
 * @Version 1.0
 **/
@RestController
@RequestMapping(value = "/news")
@Tag(name = "新闻控制器")
@Slf4j
public class NewsController {

    @Autowired
    private SmartNewsService smartNewsService;

    @Autowired
    private MinioService minioService;

    @Operation(summary = "分页查询新闻")
    @Parameters({
            @Parameter(name = "t",description = "新闻类型",required = false),
            @Parameter(name = "n",description = "页码",required = false),
            @Parameter(name = "s",description = "条数",required = false)
    })
    @RequestMapping(value = "/type",method = RequestMethod.GET)
    public ResultUtils GetNewsType(@RequestParam(value = "t",required = false,defaultValue = "1") Integer type,
                                   @RequestParam(value = "n",required = false,defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "s",required = false,defaultValue = "5") Integer pageSize)
    {
        PageBeanVO<SmartNewsVO> smartNewsDTOPageBeanVO = smartNewsService.GetSmartNews(type, pageNum, pageSize);
        return ResultUtils.success(smartNewsDTOPageBeanVO);
    }

    @Operation(summary = "上传新闻文件接口")
    @Parameter(name = "file",description = "文件",required = true)
    @RequestMapping(value = "/upFiles",method = RequestMethod.POST)
    public ResultUtils uploadFiles(MultipartFile file)
    {
        try {
            //这里返回的是 已经加密过后文件的名字
            String s = minioService.uploadFile(file);
            return ResultUtils.success(200,"上传成功",s);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Operation(summary = "修改新闻数据")
    @Parameter(name="smartNewsUpdateDTO",description = "修改新闻DTO",required = true)
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public ResultUtils updateNewsDate(@RequestBody SmartNewsUpdateDTO smartNewsUpdateDTO)
    {
        log.info(smartNewsUpdateDTO.toString());
        Integer i = smartNewsService.UpdateSmartNews(smartNewsUpdateDTO);
        return ResultUtils.success(i);
    }

    @Operation(summary = "删除新闻数据")
    @Parameter(name = "ids",description = "新闻id",required = true)
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public ResultUtils deleteNewsData(@RequestParam("ids") Long ids)
    {
        Integer i = smartNewsService.deleteSmartNews(ids);
        return ResultUtils.success(i);
    }

    @Operation(summary = "导出新闻数据")
    @Parameter(name = "type",description = "类型",required = true)
    @RequestMapping(value = "/export/data",method = RequestMethod.GET)
    public ResultUtils exportNewsData(@RequestParam("type") Integer type, HttpServletResponse response) throws IOException {
        List<SmartNewsVO> smartNewsVOS = smartNewsService.SelectSmartNewsByType(type);
        long l = System.currentTimeMillis();
        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("News", "UTF-8").replaceAll("\\+", "%20");
//        response.setHeader("Content-Disposition", "attachment; filename*=utf-8''" + fileName + ".xlsx");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName+".xlsx");

        try {
            FastExcel.write(response.getOutputStream(),SmartNewsExcelExportVO.class)
                    .sheet("模板")
                    .doWrite(smartNewsVOS);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        long d = System.currentTimeMillis();
        log.info(String.valueOf(d-l)+"秒");
        return ResultUtils.success(d-l);
    }

    @Operation(summary = "新增新闻数据")
    @Parameter(name = "smartNewsInsertDTO",description = "新增新闻DTO",required = true)
    @RequestMapping(value = "/insert/data",method = RequestMethod.POST)
    public ResultUtils insertNewsData(@RequestBody SmartNewsInsertDTO smartNewsInsertDTO)
    {
        Integer i = smartNewsService.InsertNews(smartNewsInsertDTO);
        if(i>0)
        {
            return ResultUtils.success("新增成功!");
        }
        return ResultUtils.success("新增失败!");
    }

    @PostMapping(value = "/sss")
    public String wifi()
    {
        return "恭喜你获取到了wifi密码";
    }
}
